//
//  WKNewFeatrueController.swift
//  WKWeibo
//
//  Created by 恋guang年 on 16/3/27.
//  Copyright © 2016年 恋guang年. All rights reserved.
//

import UIKit
let ReuseIdentifier = "cell"
class WKNewFeatrueController: UICollectionViewController {
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //流水布局
    let layout = UICollectionViewFlowLayout()
    
    // 父类没有init()这个构造函数,子类不需要使用overridr
    init(){
        super.init(collectionViewLayout:layout)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        prepareForCollectionView()
    }
    private func prepareForCollectionView(){
        collectionView?.registerClass(WKNewFeatrueCell.self, forCellWithReuseIdentifier: ReuseIdentifier)
        //设置layout属性
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        //设置collectionView属性
        collectionView?.bounces = false
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        
    }
}

//MARK:- 数据源和代理
///extension
extension WKNewFeatrueController{
    ///  section返回item
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return WKNewFeatruePageCount
    }
    ///  每个item的cell
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(ReuseIdentifier, forIndexPath: indexPath) as! WKNewFeatrueCell
        cell.backgroundColor = UIColor.randomColor()
        //给cell 页码 index 赋值
        cell.index = indexPath.item
        return cell
    }
    /// 滚动结束  调用cell按钮的动画
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        //获得页码
        let pageNum = Int(scrollView.contentOffset.x / UIScreen.mainScreen().bounds.width)
        let indexPath = NSIndexPath(forItem: pageNum, inSection: 0)
        //根据页码得到要显示的cell 调用cell的按钮动画方法
        let cell = collectionView?.cellForItemAtIndexPath(indexPath) as! WKNewFeatrueCell
        if pageNum ==  WKNewFeatruePageCount - 1{
            //到最后一张的时候按钮不隐藏 调用按钮动画
            cell.startButton.hidden = false
            cell.startButtonAnimation()
        }
        
    }
}


//MARK:- 自定义cell
class WKNewFeatrueCell:UICollectionViewCell{
    //属性 页码
    var index: Int = 0 {
        //属性监听器(存数型属性) index发送改变后调用
        didSet{
            //设置图片的时候就隐藏按钮 防止重用cell的时候不该出现按钮的地方出现按钮
            startButton.hidden = true
            iconView.image = UIImage(named: "new_feature_\(index+1)")
        }
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareForUI()
    }
    ///按钮出厂动画
    func startButtonAnimation(){
        ///  根据按钮的transform进行带弹簧效果的(usingSpringWithDamping)缩放动画
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.startButton.transform = CGAffineTransformIdentity
            }, completion: nil)
    }
    ///  开始按钮点击事件
    @objc private func startButtonDidClick(){
        //切换AppDelegate的根控制器为欢迎界面控制器
        AppDelegate.outSwitchAppDelegateRootViewController(WKWelcomeController())
    }
    ///添加子控件 并添加约束
    private func prepareForUI(){
        //添加子控件
        contentView.addSubview(iconView)
        contentView.addSubview(startButton)
        //添加约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
        }
        startButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView)
            make.centerY.equalTo(contentView.snp_bottom).offset(-180)
        }
    }
    /// 懒加载 按钮和背景
    private lazy var iconView:UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    private lazy var startButton: UIButton = {
        let button = UIButton()
        button.setTitle("开始体验", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        //添加按钮点击事件 点击后进入主控制器界面
        button.addTarget(self, action:"startButtonDidClick" , forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
}







